package fileIO;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import data.FilePath;
import data.ConfigInfo;

public class ConfigIO {
	public static void loadConfig(){
		try {
			BufferedReader reader = new BufferedReader(new FileReader(FilePath.CONFIG_FILE));
			while(reader.ready()){
				String line = reader.readLine();
				String[] list = line.split(" ");
				if(list.length>1){
					String title = list[0];
					if(title.equals("SERVER_ADDRESS")){
						ConfigInfo.SERVER_ADDRESS = (list[1]);
					}else if(title.equals("SENDER_ADDRESS")){
						ConfigInfo.SENDER_EMAIL = (list[1]);
					}else if(title.equals("SENDER_NAME")){
						String tempName = "";
						for(int i=1;i<list.length;i++){
							if (i==1)
								tempName = list[i];
							else
								tempName = tempName + " " + list[i];
						}
						ConfigInfo.SENDER_NAME = (tempName);
					}else if(title.equals("SECOND_REMINDER")){
						ConfigInfo.EMAIL_SECOND_REMINDER.add(list[1]);
					}else if(title.equals("THIRD_REMINDER")){
						ConfigInfo.EMAIL_THIRD_REMINDER.add(list[1]);
					}else if(title.equals("SENDER_EXT")){
						ConfigInfo.SENDER_EXT = (list[1]);
					}else if(title.equals("MAXIMIZED")){
						try{
							ConfigInfo.MAXIMIZED = Integer.parseInt(list[1]);
						}catch(NumberFormatException e){
						}
					}else if(title.equals("SEND_TO_SELF")){
						ConfigInfo.SEND_TO_SELF = Boolean.parseBoolean(list[1]);
					}else if(title.equals("COPY")){
							ConfigInfo.ADDITIONAL_EMAIL_LIST.add(list[1]);
					}else if(title.equals("SAVE_DIR")){
						ConfigInfo.SAVE_DIRECTORY = line.replace("SAVE_DIR ", "");
					}else if(title.equals("INPUT_DIR")){
						ConfigInfo.INPUT_DIRECTORY = line.replace("INPUT_DIR ", "");
					}else if(title.equals("EXCEPT_DIR")){
						ConfigInfo.EXCEPTION_DIRECTORY = line.replace("EXCEPT_DIR ", "");
					}else if(title.equals("LAST_OPEN_TEMPLATE")){
						ConfigInfo.LAST_OPEN_TEMPLATE = list[1];
					}else if(title.equals("LAST_USED_MAPPING")){
						ConfigInfo.LAST_USED_MAPPING = line.replace("LAST_USED_MAPPING ", "");
					}else if(title.equals("AUTO_RUN_SAVE_DIR")){
						ConfigInfo.AUTO_RUN_SAVE_DIR = line.replace("AUTO_RUN_SAVE_DIR ", "");
					}else if(title.equals("AUTO_RUN_INBOUND_FILE")){
						ConfigInfo.AUTO_RUN_INBOUND_FILE = line.replace("AUTO_RUN_INBOUND_FILE ", "");
					}else if(title.equals("AUTO_RUN_INBOUND_DIR")){
						ConfigInfo.AUTO_RUN_INBOUND_DIR = line.replace("AUTO_RUN_INBOUND_DIR ", "");
					}else if(title.equals("AUTO_RUN_STOP_ON_ERROR")){
						ConfigInfo.AUTO_RUN_STOP_ON_ERROR = Boolean.parseBoolean(list[1]);
					}else if(title.equals("AUTO_RUN_DELETE_ON_USE")){
						ConfigInfo.AUTO_RUN_DELETE_ON_USE = Boolean.parseBoolean(list[1]);
					}
				}
			}
		} catch (FileNotFoundException e) {
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static boolean saveConfig(){
		try{
			FileWriter writer = new FileWriter(FilePath.CONFIG_FILE);
			writer.write("SERVER_ADDRESS " + ConfigInfo.SERVER_ADDRESS + "\n");
			writer.write("SENDER_ADDRESS " + ConfigInfo.SENDER_EMAIL + "\n");
			writer.write("SENDER_NAME " + ConfigInfo.SENDER_NAME + "\n");
			writer.write("SENDER_EXT " + ConfigInfo.SENDER_EXT + "\n");
			writer.write("MAXIMIZED " + ConfigInfo.MAXIMIZED + "\n");
			writer.write("SEND_TO_SELF " + Boolean.toString(ConfigInfo.SEND_TO_SELF) + "\n");
			writer.write("LAST_OPEN_TEMPLATE " + ConfigInfo.LAST_OPEN_TEMPLATE + "\n");
			writer.write("SAVE_DIR " + ConfigInfo.SAVE_DIRECTORY + "\n");
			writer.write("INPUT_DIR " + ConfigInfo.INPUT_DIRECTORY + "\n");
			writer.write("EXCEPT_DIR " + ConfigInfo.EXCEPTION_DIRECTORY + "\n");
			writer.write("LAST_USED_MAPPING " + ConfigInfo.LAST_USED_MAPPING + "\n");
			writer.write("AUTO_RUN_SAVE_DIR " + ConfigInfo.AUTO_RUN_SAVE_DIR + "\n");
			writer.write("AUTO_RUN_INBOUND_FILE " + ConfigInfo.AUTO_RUN_INBOUND_FILE + "\n");
			writer.write("AUTO_RUN_INBOUND_DIR " + ConfigInfo.AUTO_RUN_INBOUND_DIR + "\n");
			writer.write("AUTO_RUN_STOP_ON_ERROR " + Boolean.toString(ConfigInfo.AUTO_RUN_STOP_ON_ERROR) + "\n");
			writer.write("AUTO_RUN_DELETE_ON_USE " + Boolean.toString(ConfigInfo.AUTO_RUN_DELETE_ON_USE) + "\n");
			for(int i=0;i<ConfigInfo.ADDITIONAL_EMAIL_LIST.size();i++){
				writer.write("COPY " + ConfigInfo.ADDITIONAL_EMAIL_LIST.get(i)+ "\n");
			}
			for(int i=0;i<ConfigInfo.EMAIL_SECOND_REMINDER.size();i++){
				writer.write("SECOND_REMINDER " + ConfigInfo.EMAIL_SECOND_REMINDER.get(i)+ "\n");
			}
			for(int i=0;i<ConfigInfo.EMAIL_THIRD_REMINDER.size();i++){
				writer.write("THIRD_REMINDER " + ConfigInfo.EMAIL_THIRD_REMINDER.get(i)+ "\n");
			}
			writer.close();
			return true;
		}catch(FileNotFoundException e){
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
}
